<template>
  <div class="tab-pane-wrapper">
    <page-table
      :tableData="tableData"
      :tableLabel="colSData"
      :showSelection="false"
      :pager="pager"
      showIndex
      :table-height="tableHeight"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
      @handleButton="handleButton(arguments)"
    ></page-table>
    <pub-dialog
      title="区间标准"
      type="big"
      top="15vh"
      :sureBtnLoading="loading"
      :dialogVisible="dialogVisible"
      dialogVisableRef="dialogSateVisible"
      @switchVisable="switchVisable"
      @dialogSubmit="dialogSubmit"
      @dialogCancel="dialogCancel"
      btnType="3"
    >
      <el-form
        class="form-search"
        label-width="90px"
        :rules="rules"
        ref="form"
        :model="formData"
      >
        <el-form-item label="执行标准" prop="sectionStandard">
          <el-input
            placeholder="请输入"
            v-model="formData.sectionStandard"
          ></el-input>
        </el-form-item>
        <el-form-item label="处理脚本" prop="sectionScript">
          <el-input
            placeholder="请输入"
            type="textarea"
            :rows="3"
            v-model="formData.sectionScript"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="paramKey">
          <p>历史脚本</p>
          <div class="detail">
            <p
              @click="handleRecord(item)"
              v-for="(item, index) in formData.record"
              :key="index"
            >
              {{ item }}
            </p>
          </div>
        </el-form-item>
      </el-form>
    </pub-dialog>
  </div>
</template>
<script>
import PageTable from "@/components/PageTable";
import pubDialog from "@/components/pubDialog.vue";
import {
  xjAssessList,
  xjAssessInitialize,
  xjAssessSetSection,
} from "@/api/pointMaintain";
export default {
  name: "sonModulePro",
  props: {
    siteId: {
      type: String,
      default: "",
    },
  },
  components: {
    pubDialog,
    PageTable,
  },
  data() {
    return {
      tableData: [],
      colSData: [
        { label: "测点名称", param: "measureName" },
        {
          label: "操作",
          param: "operate",
          width: 300,
          operate: [
            {
              label: "执行标准",
              click: "action",
              type: "text",
            },
            {
              label: "初始化",
              click: "init",
              type: "text",
            },
          ],
        },
      ],
      pager: {
        pageSize: 10, // 每页数据量
        currentPage: 1, // 分页  当前页
        total: 0, //列表总数
      },
      tableHeight: 400,
      formData: {
        sectionStandard: "",
        sectionScript: "",
        record: [],
      },
      rules: {},
      dialogVisible: false,
      loading: false,
    };
  },
  created() {
    this.calcHeight();
    this.getList();
  },
  mounted() {},
  methods: {
    calcHeight() {
      this.tableHeight = window.innerHeight - 180;
    },
    handleSizeChange(val) {
      this.pager.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pager.currentPage = val;
      this.getList();
    },
    handleButton(method, row, index) {
      if (method[0] === "init") {
        this.$confirm("确认初始化子模块状态?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            xjAssessInitialize({ id: method[1].id }).then((res) => {
              if (res.code == 20000) {
                this.$message({
                  type: "success",
                  message: "初始化成功!",
                });
                this.getList();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消初始化",
            });
          });
      } else {
        // this.formData = { ...method[1] };
        for (let key in this.formData) {
          this.formData[key] = method[1][key];
        }
        this.formData.id = method[1].id;
        this.dialogVisible = true;
      }
    },

    async getList() {
      let params = {
        siteId: this.siteId,
      };
      let result = await xjAssessList(params);
      if (result.code == 20000) {
        this.tableData = result.data;
        // this.pager.total = Number(result.data.total);
      }
    },
    switchVisable(visable, ref) {
      this[ref] = visable;
    },
    dialogCancel() {
      this.dialogVisible = false;
    },
    handleRecord(item) {
      this.formData.sectionScript = item;
    },
    // 保存添加或者编辑测点
    dialogSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          let params = { ...this.formData };
          delete params.record;
          xjAssessSetSection(params)
            .then((res) => {
              if (res.code == 20000) {
                this.dialogVisible = false;
                this.getList();
                this.$message.success("保存成功");
              }
              this.loading = false;
            })
            .catch((err) => {
              this.loading = false;
              return err;
            });
        }
      });
    },
  },
};
</script>
<style scoped lang="less">
::v-deep .form-search .el-form-item {
  margin-bottom: 22px;
}
::v-deep .el-dialog__body {
  height: 50vh;
  overflow-y: hidden;
  .el-form.form-search {
    height: 100%;
    overflow-y: hidden;
  }
}
.tab-pane-wrapper {
  height: 100%;
  width: 100%;
  padding: 20px;
  background-color: #202f3e;
  .form-search {
    margin-bottom: 16px;
    p {
      font-size: 16px;
      font-weight: bold;
      line-height: 32px;
      text-align: center;
      color: #c6d3ec;
      border: 1px solid #40e5f0;
    }
    .detail {
      height: calc(50vh - 254px);
      overflow-y: auto;
      p {
        font-weight: normal;
        font-size: 12px;
        border-bottom: 0;
        cursor: pointer;

        &:last-child {
          border-bottom: 1px solid #40e5f0;
        }
      }
    }
  }
}
</style>
